package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import com.yonyou.pmclouds.workConnect.entity.WorkConReceiverVO;
import com.yonyou.pmclouds.workConnect.entity.WorkConnectVO;
import com.yonyou.pmclouds.workConnect.service.rmiitf.WorkConnectQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 工作联系通用接口
 */
@Component
public class WorkConnectBuziServiceImpl implements IBillViewBuziService {

    @Autowired
    private WorkConnectQuery workConnectQuery;

    @Override
    public String getBillType() {
        return BillTypeConst.WORKCONNECT_BILLTYPE;
    }

    /**
     * 设置工作联系必输项
     * @param billViewQueryVO
     * @return
     * @throws BusinessException
     */
    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {
        String startTime = StringUtils.isNotBlank(billViewQueryVO.getStartTime()) ?billViewQueryVO.getStartTime() + " 00:00:00" : "1000-01 00:00:00";
        String endTime = StringUtils.isNotBlank(billViewQueryVO.getEndTime()) ? billViewQueryVO.getEndTime() + " 23:59:59" :"2090-12-31 23:59:59";
        billViewQueryVO.setStartTime(startTime);
        billViewQueryVO.setEndTime(endTime);
        PageObject<Object> pageResult = workConnectQuery.queryByCondition(billViewQueryVO);

        setBillStatus(pageResult.getObjects());

        return pageResult;

    }

    /**
     * 查询工作联系详情
     * @param pkBill
     * @return
     * @throws BusinessException
     */
    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {

        WorkConnectVO resultVO = workConnectQuery.selectOne(pkBill);
        setBillStatus(new WorkConnectVO[]{resultVO});
        BillViewUtil.setFormatDate(new WorkConnectVO[]{resultVO},new String[]{CommonFieldConst.CREATIONTIME},new String[]{"strCreationtime"});

        return resultVO;
    }

    /**
     * 设置工作联系状态
     * @param workConnectVOs
     */
    private void setBillStatus(Object[] workConnectVOs){

        for (Object buziVO :   workConnectVOs) {
            byte billStatus = (byte) BeanHelper.getProperty(buziVO, CommonFieldConst.BILLSTATUS);
            String strBillStatus = "";
            switch (billStatus){
                case 1 :
                    strBillStatus = WorkConReceiverVO.RECEIVE_STATUS_UNACCEPT_NAME;
                    break;
                case 2 :
                    strBillStatus = WorkConReceiverVO.RECEIVE_STATUS_ACCEPTED_NAME;
                    break;

                default:strBillStatus="";
            }
            BeanHelper.setProperty(buziVO,"strBillstatus",strBillStatus);
        }

    }
}
